﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Sonic.Framework.Extensions
{
    public static class XAttributeExtensions
    {
        /// <summary>
        /// This method checks to see if the current XAttribute has a value. If it does, the method
        /// returns that value, if not, it will return the defaultValue passed into the method.
        /// </summary>
        /// <param name="xattribute">XAttribute to check for a value</param>
        /// <param name="defaultValue">The default value to return if the current XAttribute does not have a value of its own</param>
        /// <returns></returns>
        public static string ValueOrDefault(this XAttribute xattribute, string defaultValue)
        {
            return (xattribute == null ? defaultValue : xattribute.Value);
        }

        /// <summary>
        /// This method checks to see if the current XAttribute has a value. If it does, the method
        /// returns that value, if not, it will return the defaultValue passed into the method.
        /// </summary>
        /// <param name="xattribute">XAttribute to check for a value</param>
        /// <param name="defaultValue">The default string array to return if the current XAttribute does not have a value of its own</param>
        /// <param name="delimiter">A delimeter used to split the current XAttribute's value into a string array</param>
        /// <returns></returns>
        public static string[] ValueOrDefault(this XAttribute xattribute, string[] defaultValue, char delimiter)
        {
            return (xattribute == null ? defaultValue : xattribute.Value.Split(delimiter));
        }

        /// <summary>
        /// This method checks to see if the current XAttribute has a value. If it does, the method
        /// returns that value, if not, it will return the defaultValue passed into the method.
        /// </summary>
        /// <param name="xattribute">XAttribute to check for a value</param>
        /// <param name="defaultValue">The default value to return if the current XAttribute does not have a value of its own</param>
        /// <returns></returns>
        public static bool ValueOrDefault(this XAttribute xattribute, bool defaultValue)
        {
            return (xattribute == null ? defaultValue : bool.Parse(xattribute.Value));
        }

        /// <summary>
        /// This method checks to see if the current XAttribute has a value. If it does, the method
        /// returns that value, if not, it will return the defaultValue passed into the method.
        /// This method also converts that value into an Enum.
        /// </summary>
        /// <param name="xattribute">XAttribute to check for a value</param>
        /// <param name="defaultValue">The default Enum value to return if the current XAttribute does not have a value of its own</param>
        /// <param name="enumType">The enum type of the return value</param>
        /// <returns></returns>
        public static Enum ValueOrDefault(this XAttribute xattribute, Enum defaultValue, Type enumType)
        {
            return (Enum) (xattribute == null ? defaultValue : Enum.Parse(enumType, xattribute.Value));
        }
    }
}
